﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace ChineseChess.Game
{
    [Serializable]
    /// <summary>
    /// 棋子类
    /// </summary>
    public class Piece : Shape, ICloneable
    {
        /// <summary>
        /// 棋子所遵守的规则
        /// </summary>
        public Rule rule
        {
            get;
            set;
        }
        public Piece()
        {

        }
        public int type;
        public int xgap;
        public int ygap;
        public Piece(int type,int xgap, int ygap)
        {
            this.type = type;
            if (type == 1)
            {
                this.ColorType = Colors.Red;
                this.BorderColor = Colors.Black;
                isRed = true; 
            }
            else if (type == 2)
            {
                this.ColorType = Colors.Black;
                this.BorderColor = Colors.Red;
                isRed = false;
            }

            this.xgap = xgap;
            this.ygap = ygap;
            base.Width = 52;
            base.Height = 52;
            this.RenderTransform = new TranslateTransform();
        }
        protected override System.Windows.Media.Geometry DefiningGeometry
        {
            get
            {
                EllipseGeometry r = new EllipseGeometry(new Rect(0, 0, base.Width, base.Height));
                
                return r;
            }
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            string testString = Name.ToString();
            FormattedText formattedText = new FormattedText(
               testString,
               CultureInfo.GetCultureInfo("en-us"),
               FlowDirection.LeftToRight,
               new Typeface("Verdana"),
               32,
               new SolidColorBrush(ColorType));

            drawingContext.DrawEllipse(new SolidColorBrush(BorderColor), new Pen(), new Point(0,0), Width / 2, Height / 2);
            drawingContext.DrawEllipse(new SolidColorBrush(Color.FromRgb(200, 140, 100)), new Pen(), new Point(0,0), Width / 2 - 2, Height / 2 - 2);
            drawingContext.DrawText(formattedText, new Point(-17,-18));
        }

        /// <summary>
        /// 是否为红方
        /// </summary>
        public bool isRed;

        private int _x;
        /// <summary>
        /// 当前棋子横坐标
        /// </summary>
        public int X {
            get
            {
                return _x;
            }
            set
            {
                _x = value;
                this.Margin = new System.Windows.Thickness(xgap * this._x, ygap * this._y, 0, 0);
            }
        }

        private int _y;
        /// <summary>
        /// 当前棋子纵坐标
        /// </summary>
        public int Y {
            get
            {
                return _y;
            }
            set
            {
                _y = value;
                this.Margin = new System.Windows.Thickness(xgap * this._x, ygap * this._y, 0, 0);
            }
        }

        /// <summary>
        /// 当前棋子名称
        /// </summary>
        public ChessName Name { get; set; }

        public Color ColorType { get; set; }
        public Color BorderColor { get; set; }

        public void setLoc(int left,int top)
        {
            this.Margin = new System.Windows.Thickness(left, top, 0, 0);  
        }
        public void setXy(int x,int y)
        {
            this.X = x;
            this.Y = y;
            this.Margin = new System.Windows.Thickness(xgap * this.X, ygap * this.Y, 0, 0);
        } 

        public int getGapX()
        {
            return xgap * this.X;
        }
        public int getGapY()
        {
            return ygap * this.Y;
        }
         

        public object Clone()
        {
            return new Piece(type, xgap, ygap) as object;//深复制  
        }
    }
}
